﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.DataClass;
using System.Data.SqlClient;
using SMALLERP.ComClass;
using SMALLERP;
using XJLSC.PM;
using XJLSC.QM;


namespace XJLSC.PM
{
    public partial class CJSGD : Form
    {
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr1;
        CHAXUN f;
        public CHAXUN.Cdgzzx CHAXUN_Load { get; set; }
        public CJSGD()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {

                if (this.dataGridView1.CurrentCell.Value.ToString() != "")
                {
                    if (e.ColumnIndex.Equals(this.dataGridView1.Columns["TB003"].Index))
                    {
                        string strSql = null;
                        strSql = "SELECT TB003,INVMB.UDF01,MB002,MB003 FROM MOCTB LEFT JOIN INVMB ON TB003=MB001  WHERE TB003 ='" + this.dataGridView1.CurrentCell.Value.ToString() + "' AND TB001='" + dataGridView1["TA001", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TB002='" + dataGridView1["TA002", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'";

                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["TB004"].Index].Value = sdr1["UDF01"].ToString();
                                this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["MB002"].Index].Value = sdr1["MB002"].ToString();
                                this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["MB003"].Index].Value = sdr1["MB003"].ToString();
                            }

                            if (sdr1.HasRows == false)
                            {
                                MessageBox.Show("该材料品号不存在该工单里！", "软件提示");
                                this.dataGridView1.CurrentCell.Value = "";
                            }

                        }

                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }
                    }
                       //输入老编码时
                    if (e.ColumnIndex.Equals(this.dataGridView1.Columns["TB004"].Index))
                    {
                        string strSql = null;
                        strSql = "SELECT TB003,INVMB.UDF01,MB002,MB003 FROM MOCTB LEFT JOIN INVMB ON TB003=MB001  WHERE INVMB.UDF01 LIKE '%" + this.dataGridView1.CurrentCell.Value.ToString() + "%' AND TB001='" + dataGridView1["TA001", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TB002='" + dataGridView1["TA002", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "'";

                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {
                                this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["TB003"].Index].Value = sdr1["TB003"].ToString();
                                this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["MB002"].Index].Value = sdr1["MB002"].ToString();
                                this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["MB003"].Index].Value = sdr1["MB003"].ToString();
                            }

                            if (sdr1.HasRows == false)
                            {
                                MessageBox.Show("该材料品号不存在该工单里！", "软件提示");
                                this.dataGridView1.CurrentCell.Value = "";
                            }

                        }

                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }
                    }

                    //输入工单单号时
                    if (e.ColumnIndex.Equals(this.dataGridView1.Columns["TA002"].Index))
                    {
                        string clph = (dataGridView1["TB003", dataGridView1.CurrentCell.RowIndex].Value == null) ? "" : dataGridView1["TB003", dataGridView1.CurrentCell.RowIndex].Value.ToString();

                        string strSql = null;
                        strSql = "SELECT * FROM MOCTA WHERE TA001='" + dataGridView1["TA001", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TA002='" + dataGridView1["TA002", dataGridView1.CurrentCell.RowIndex].Value.ToString() + "' AND TA011<>'Y' AND TA011<>'y' AND EXISTS(SELECT * FROM MOCTB WHERE TB001=TA001 AND TB002=TA002 AND (TB003='" + clph + "' OR '" + clph + "'=''))";

                        try
                        {
                            sdr1 = db.GetDataReader1(strSql);

                            while (sdr1.Read())
                            {

                            }

                            if (sdr1.HasRows == false)
                            {
                                MessageBox.Show("该工单不需要这个物料或者已完工，请检查！", "软件提示");
                                this.dataGridView1.CurrentCell.Value = "";
                            }

                        }

                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }
                        finally
                        {
                            sdr1.Close();
                        }
                    }

                    if (e.ColumnIndex.Equals(this.dataGridView1.Columns["TB005"].Index))
                    {
                        if (Convert.ToInt16(this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["TB007"].Index].Value) == 0)
                        {
                            this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["TB007"].Index].Value = this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["TB005"].Index].Value;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            string gddb = null;
            string sgr = null;
           
            if (this.dataGridView1.CurrentRow.Index > 0)
            {
                gddb = (this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["TA001"].Index].Value == null) ? "" : this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["TA001"].Index].Value.ToString();
                sgr = (this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["TB008"].Index].Value == null) ? "" : this.dataGridView1.CurrentRow.Cells[this.dataGridView1.Columns["TB008"].Index].Value.ToString();
            
                if (gddb == "")
                {
                    this.dataGridView1.Rows[this.dataGridView1.CurrentRow.Index].Cells[this.dataGridView1.Columns["TA001"].Index].Value = this.dataGridView1.Rows[this.dataGridView1.CurrentRow.Index - 1].Cells[this.dataGridView1.Columns["TA001"].Index].Value;
                }

                if (sgr == "")
                {
                    this.dataGridView1.Rows[this.dataGridView1.CurrentRow.Index].Cells[this.dataGridView1.Columns["TB008"].Index].Value = this.dataGridView1.Rows[this.dataGridView1.CurrentRow.Index - 1].Cells[this.dataGridView1.Columns["TB008"].Index].Value;
                }

            }
        
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strSql = null;
            string qgdh = "";
            strSql = "SELECT CONVERT(CHAR(8),GETDATE(),112)+  ISNULL(right('000'+rtrim(cast(cast(MAX(SUBSTRING(TA002,9,3)) as numeric(16))+1 as char(3))),3),'001') AS QGDH  from PURTA WHERE TA002 LIKE CONVERT(CHAR(8),GETDATE(),112)+'%'  AND TA001='3110'";
            try
            {
                sdr1 = db.GetDataReader1(strSql);
                while (sdr1.Read())
                {
                    qgdh = sdr1["QGDH"].ToString();
                }
                if (sdr1.HasRows == false)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                return;
            }
            finally
            {
                sdr1.Close();
            }

            if (checkBox1.CheckState == CheckState.Checked)
            {
                strSql = "INSERT INTO PURTA(COMPANY,CREATOR,USR_GROUP,CREATE_DATE,MODIFIER,MODI_DATE,FLAG,TA001,TA002,TA003,TA004,TA005,TA006,TA007,TA008,TA009,TA010,TA011,TA012,TA013,TA014,TA015,TA016,TA017,TA018,TA019,TA020,TA021,TA022,TA023,TA024,TA025,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60,UDF61,UDF62) ";
                strSql = strSql + "SELECT 'ZJHS','" + PropertyClass.OperatorCode + "','',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'','','1','3110','" + qgdh + "',CONVERT(CHAR(8),GETDATE(),112),'','','车间申购','N',0,'9','1000',0,'',CONVERT(CHAR(8),GETDATE(),112),'',0,'N',0,'','','',0,0,0,0,'','','','','','','',0,0,0,0,0,0,'','','','','','',0,0,0,0,0,0";
                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {
                    }
                    else
                    {
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }
            }
            for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            {
                string gddb = "";
                string gddh = "";
                string clph = "";
                string clpm = "";
                string clgg = "";
                string cllbm = "";
                string sgr = "";
                string sgbz = "";
                double qgsl = 0;

                gddb = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["TA001"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["TA001"].Index].Value.ToString();
                gddh = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["TA002"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["TA002"].Index].Value.ToString();
                clph = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["TB003"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["TB003"].Index].Value.ToString();
                clpm = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["MB002"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["MB002"].Index].Value.ToString();
                clgg = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["MB003"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["MB003"].Index].Value.ToString();
                cllbm = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["TB004"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["TB004"].Index].Value.ToString();
                sgr = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["TB008"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["TB008"].Index].Value.ToString();
                sgbz = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["TB009"].Index].Value == null) ? "" : this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["TB009"].Index].Value.ToString();

                qgsl = (this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["TB007"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["TB007"].Index].Value);

                strSql = "UPDATE MOCTB SET TB004=TB004+" + qgsl + ",UDF57=UDF57+" + qgsl + " WHERE TB001='" + gddb + "' AND TB002='" + gddh + "' AND TB003='" + clph + "'";
                try
                {
                    if (db.ExecDataBySql1(strSql) > 0)
                    {

                    }
                    else
                    {

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    return;
                }

                if (checkBox1.CheckState == CheckState.Checked)
                {
                   
                    strSql = "INSERT INTO PURTB(COMPANY,CREATOR,USR_GROUP,CREATE_DATE,MODIFIER,MODI_DATE,FLAG,TB001,TB002,TB003,TB004,TB005,TB006,TB007,TB008,TB009,TB010,TB011,TB012,TB013,TB014,TB015,TB016,TB017,TB018,TB019,TB020,TB021,TB022,TB023,TB024,TB025,TB026,TB027,TB028,TB029,TB030,TB031,TB032,TB033,TB034,TB035,TB036,TB037,TB038,TB039,TB040,TB041,TB043,TB044,TB045,TB046,TB047,TB048,TB049,TB050,TB052,TB053,TB054,TB055,TB056,TBC01,TB057,TB058,TB059,TB060,UDF01,UDF02,UDF03,UDF04,UDF05,UDF06,UDF51,UDF52,UDF53,UDF54,UDF55,UDF56,UDF07,UDF08,UDF09,UDF10,UDF11,UDF12,UDF57,UDF58,UDF59,UDF60,UDF61,UDF62) ";
                    strSql = strSql + "SELECT 'ZJHS','" + PropertyClass.OperatorCode + "','',REPLACE(REPLACE(REPLACE(convert(char(20),getdate(),120),'-',''),':',''),' ',''),'','','1','3110','" + qgdh + "',right('000'+'" + (i+1) + "',4),'" + clph + "','" + clpm + "','" + clgg + "',MB004,MB017,'" + qgsl + "',MB032,CONVERT(CHAR(8),GETDATE(),112),'" + sgbz + "',MB067," + qgsl + ",MB004,'RMB',0,0,CONVERT(CHAR(8),GETDATE(),112),'N','N','','','','N','1','','','" + gddb + "','" + gddh + "','','Y','',0,0,'',MB090,MB090,'N',0,0,MB149," + qgsl + "," + qgsl + ",0,0," + qgsl + ",'',CONVERT(CHAR(8),GETDATE(),112),0,0,0,'',0,'2','','','','','','','','','','',0,0,0,0,0,0,'','','','','','',0,0,0,0,0,0 FROM INVMB WHERE MB001='" + clph + "'";
                    try
                    {
                        if (db.ExecDataBySql1(strSql) > 0)
                        {
                        }
                        else
                        {
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        return;
                    }

                }
            }
            MessageBox.Show("申购成功!", "软件提示");
            dataGridView1.Rows.Clear();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void CJSGD_Load(object sender, EventArgs e)
        {
            checkBox1.CheckState = CheckState.Checked;
        }

        private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            this.dataGridView1.Controls.Clear();//移除所有控件 
            if (e.ColumnIndex.Equals(this.dataGridView1.Columns["TB003"].Index))
            //if (e.ColumnIndex == 13)
            //判断单元格是否是"Company"列？    
            {
                System.Windows.Forms.Button btn = new System.Windows.Forms.Button();//创建Buttonbtn    
                btn.Text = "...";//设置button文字    
                btn.Font = new System.Drawing.Font("Arial", 5);//设置文字格式    
                btn.Visible = true;//设置控件允许显示    

                btn.Width = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,
                                e.RowIndex, true).Height;//获取单元格高并设置为btn的宽    
                btn.Height = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex,
                                e.RowIndex, true).Height;//获取单元格高并设置为btn的高    

                btn.Click += new EventHandler(btn_Click);//为btn添加单击事件    

                this.dataGridView1.Controls.Add(btn);//dataGridView1中添加控件btn    

                btn.Location = new System.Drawing.Point(((this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Right) -
                        (btn.Width)), this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Y);//设置btn显示位置    
            }
        }

        void btn_Click(object sender, EventArgs e)
        {
            string x1 = dataGridView1["TA001", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            string x2 = dataGridView1["TA002", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            double x3 = 0;
            string x4 = "材料品号开窗";
            string x5 = "";
            MXCX xx = new MXCX(x1, x2, x3, x4, x5);
             f = new CHAXUN();
            f.DataSelected += new EventHandler(f_DataSelected);
            //f.cdgzzx += new SCKC.Cdgzzx(textBox2_TextChanged);
            f.Show(this);
        }

        void f_DataSelected(object sender, EventArgs e)
        {

            this.dataGridView1.CurrentCell.Value = f.clph;
            this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.Columns["MB002"].Index].Value = f.clpm;
            this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.Columns["MB003"].Index].Value = f.clgg;
            this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.Columns["TB005"].Index].Value = f.cllbm;

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

    }
}